import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/widgets/code_input_field.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:flutter_syt/widgets/dyt_aliyun_widget.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

import '../../global_easy_loading.dart';

@RoutePage(name: "removeAccountVerify")
class RemoveAccountVerify extends StatefulWidget {
  const RemoveAccountVerify();

  @override
  _RemoveAccountVerifyState createState() => _RemoveAccountVerifyState();
}

class _RemoveAccountVerifyState extends State<RemoveAccountVerify> {
  String smsCode = '';

  Future<bool> getSmsCode() async {
    var res = false;
    bool isVertifica = await UtilsOther.checkShowVertification(
        account: (UserInfo.me.registerType == '2'
                ? UserInfo.me.email
                : UserInfo.me.phone_number) ??
            '',
        // type: UserInfo.me.registerType == '2' ? '2' : '1',
        context: context,
        status: DytAliyunStatus.DytAliyunStatusMessage);
    if (isVertifica == false) {
      return false;
    }
    try {
      res = await (UserInfo.me.registerType == '2'
          ? API.shared.login.getEmailCode(UserInfo.me.email ?? '', 'destroy')
          : API.shared.login.getPhoneCode2('destroy'));
    } catch (e) {
      await UtilsOther.showCustomShowError(e);
      // if (e is DioError) {
      //   customShowError(e.message);
      // } else {
      //   customShowError(S().network_error);
      // }
      res = false;
    }
    while (mounted) {
      return res;
    }
    return res;
    // return true;
  }

  TextStyle textStyle = FontStyles.headerTextStyle_16.copyWith(
    color: ThemeColor.colorBlack80,
    fontWeight: FontWeight.w300,
  );
  @override
  Widget build(BuildContext context) {
    UserInfo me = UserInfo.me; //Provider.of<UserInfo>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).Verify_account),
      ),
      body: Stack(
        children: [
          Container(
            color: Colors.white,
            padding: EdgeInsets.all(20),
            child: Container(
              color: Color(0xffffffff),
              child: ListView(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 40.0, left: 0.0),
                    alignment: Alignment.center,
                    child: Text(
                      UserInfo.me.registerType == '2'
                          ? S.of(context).sent_code_success_email
                          : S.of(context).sent_code_success,
                      style: TextStyle(
                        color: ThemeColor.colorBlack60,
                        fontSize: 14,
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 16.0, left: 0.0),
                    alignment: Alignment.center,
                    child: Text(
                      UserInfo.me.registerType == '2'
                          ? '${me.email}'
                          : '+${me.area_code} ${me.phone_number}',
                      style: TextStyle(
                        color: ThemeColor.colorBlack80,
                        fontSize: 18,
                      ),
                    ),
                  ),
                  Container(
                    padding:
                        EdgeInsets.only(left: 20.0, top: 24.0, right: 20.0),
                    child: CodeInputTextField(
                      codeLength: 6,
                      autoFocus: true,
                      keyboardType: TextInputType.visiblePassword,
                      getSmsCode: getSmsCode,
                      textInputAction: TextInputAction.go,
                      onSubmit: (code) {
                        print('submit code:$code');
                        setState(() {
                          smsCode = code;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
          Positioned(
            bottom: 10,
            child: Container(
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.all(20),
              child: ElevatedButton(
                  child: Text(
                    S.of(context).continues,
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  onPressed: ClickUtils.debounce(
                    () {
                      if (smsCode == '' || smsCode.length < 6) {
                        customShowToast(S.of(context).enter_6_code);
                        return;
                      }
                      RouteManager.push(
                        context,
                        RoutMapping.removeAccountConfirm(smsCode),
                      );
                    },
                  )),
            ),
          ),
        ],
      ),
    );
  }
}
